home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group96a.txt
/
000094_icon-group-sender_Wed Apr 17 12:48 MST 1996.msg
< prev
next >
Wrap
Text File
|
1996-09-05
|
2KB
|
68 lines
Date: Wed, 17 Apr 1996 17:36:13 +0100
From: Hamish Lawson <H.Lawson@tees.ac.uk>
Mime-Version: 1.0
To: icon-group@cs.arizona.edu
Subject: Splitting and joining strings
Content-Transfer-Encoding: 7bit
Before I rolled my own I thought I'd have a look in the Icon Program
Library for a couple of procedures I need.
The first should split a string on a specified separator, along the
lines of
split("Joe:Sue:Bob:Ann", ":") produces "Joe", "Sue", "Bob", "Ann"
The second should join the elements of a list into a string, with each
element separated by a specified separator, along the lines of:
join(["Joe", "Sue", "Bob", "Ann"], ":") produces "Joe:Sue:Bob:Ann"
I couldn't find any such procedures in the IPL. Have I missed them?
Here are two routines that I've got: split and atos (aggregate to string).
#
# split(s, delimiters, keepall) splits the string s into consecutive substrings
# that do/do not consist of characters in the cset delimiters, producing a
# list of strings. If keepall is null, strings consisting delimiters are not
# included in the result list.
#
# If not specified, delimeters defaults to blank and tab, which essentially
# "tokenizes" non-whitespace:
#
# words := split(read())
#
procedure split(s, dlms, keepall)
local w, ws, addproc, nullproc
ws := []
/dlms := " \t"
addproc := put
if \keepall then
otherproc := put
else
otherproc := 1
if dlms := (any(dlms, s[1]) & ~dlms) then
otherproc :=: addproc
s ? while w := tab(many(dlms := ~dlms)) do {
addproc(ws, w)
otherproc :=: addproc
}
return ws
end
procedure atos(a, delim)
local e, s
/delim := ","
every e := !a do
(/s := e) | (s ||:= delim || e)
return s
end